package com.baidu.baidumap;

import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.DrivingRouteOverlay;
import com.baidu.mapapi.overlayutil.TransitRouteOverlay;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;

import java.util.ArrayList;
import java.util.List;

/**\
 * 驾车路线
 */

public class TransitRouteOverlayDemo extends BaseActivity {
    private RoutePlanSearch routePlanSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        search();

    }

    private void search() {
        routePlanSearch = RoutePlanSearch.newInstance();
        routePlanSearch.setOnGetRoutePlanResultListener( new MyListener());
        TransitRoutePlanOption transitOption = new TransitRoutePlanOption();
        PlanNode from = PlanNode.withLocation(hmPos);// 创建起点
        PlanNode to = PlanNode.withLocation(new LatLng(40.065796,116.349868));// 创建终点
        transitOption.from(from);
        transitOption.to(to);
        transitOption.city("北京");
        transitOption.policy(TransitRoutePlanOption.TransitPolicy.EBUS_WALK_FIRST);
        routePlanSearch.transitSearch(transitOption);

    }
    class MyListener implements OnGetRoutePlanResultListener{


        @Override
        public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {


        }

        @Override
        public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
            if (transitRouteResult==null|| SearchResult.ERRORNO.RESULT_NOT_FOUND==transitRouteResult.error){
                Toast.makeText(getApplicationContext(),"未搜索到结果",Toast.LENGTH_SHORT).show();
                return;
            }
            TransitRouteOverlay overlay = new TransitRouteOverlay(baiduMap);
            baiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(transitRouteResult.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

        }
    }

    class MyDrivingOverlay extends DrivingRouteOverlay{

        public MyDrivingOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
        }
    }
}
